# -*- coding: utf-8 -*-
from com.zzh.utils.dbUtils import MysqlUtils
from com.zzh.utils.table2JSON import Table2Json
import time

PAGE_SIZE = 3
local_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))

'''
姚明 281720252@qq.com
'''


class MessageAPI(object):
    def __init__(self):
        self.conn = MysqlUtils.get_mysql_connections()

    def queryMessage(self, messageType, userId):
        cur = self.conn.cursor()
        sql = "SELECT * FROM zzh_message WHERE message_type =%s AND user_id=%s"
        rs = cur.execute(sql, [messageType, userId])

        messageListJson = None
        if rs:
            messageList = cur.fetchall()
            messageListJson = Table2Json.message_list_to_json(messageList)

        cur.close()
        self.conn.close()
        return messageListJson

    def addMessage(self, addMessage):  # process表添加记录
        messageType = addMessage['messageType']  # processChildName  子流程的名称
        messageName = addMessage['messageName']  # processChildCode 子流程步数
        messageTime = addMessage['messageTime']  # processChildStatus  子流程状态
        messageTitle = addMessage['messageTitle']
        messageContent = addMessage['messageContent']
        userId = addMessage['userId']
        cur = self.conn.cursor()
        sql = "INSERT INTO zzh_message (message_type,message_name,message_time,message_title,message_content,user_id) values (%s,%s,%s,%s,%s,%s)"
        addResultOfDatabaseMessage = cur.execute(sql,
                                                 [messageType, messageName, messageTime, messageTitle, messageContent,userId])
        self.conn.commit()
        if addResultOfDatabaseMessage:  # 插入成功，关闭数据库链接，返回1
            cur.close()
            self.conn.close()
            return 1
        else:  # 插入失败，关闭数据库链接，返回0
            cur.close()
            self.conn.close()
            return 0

    def deleteMessageById(self, messageId):
        cur = self.conn.cursor()
        sql = "DELETE from zzh_message WHERE message_id=%s"
        deleteMessageResultOfDatabase = cur.execute(sql, [messageId])
        self.conn.commit()
        if deleteMessageResultOfDatabase:  # 删除成功，关闭数据库链接，返回1
            cur.close()
            self.conn.close()
            return 1
        else:  # 删除失败，关闭数据库链接，返回0
            cur.close()
            self.conn.close()
            return 0
